﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using static QntPlatform.Strategy.TvCmdHandler;
using QntPlatform.Strategy.CmdCtrl;

namespace QntPlatform.Strategy
{
 /// <summary>
 /// 趋势做单，只添加固定止损，不设止盈，
 /// </summary>
    public class BuyStop: StrategyBase
    {

        StrategyBase Parent { get; set; }
        TvCmdHandler tvCmdEx;
        public BuyStop(IExchange exchange, ISysFun sysFun, ISysApi sysApi, ITAFun ta) : base(exchange, sysFun, sysApi, ta)
        {
        }
        public BuyStop(StrategyBase parent):base(parent.Exchange,parent.SysFun,parent.SysApi,parent.TA)
        {
            Parent = parent;
            tvCmdEx =    Parent as TvCmdHandler;
        }
        public override void Init()
        {
            tvCmdEx =    Parent as TvCmdHandler;
            tvCmdEx.cmdFunDic.Add("signSync", signSync);
            tvCmdEx.AutoHandlerDic.Add(nameof(BuyStop), AutoOutHandle);
        }
        public void signSync(string cmd)
        {
            var info = tvCmdEx.GetCmdValue<Info>(cmd);

            if (info.Direction == SideDirection.Buy)
            {
                InBuy(info);
            }
            else if (info.Direction == SideDirection.Sell)
            {
                InBuy(info, false);
            }
        }
        public void InBuy(Info info, bool isLong = true)
        {
            //buy
            var account = Exchange.GetAccountAsync();
            var tck = Exchange.GetTickerAsync();
            var nowPrice = tck.Result.Buy;
            var buyAmount = account.Result.Balance * 0.05m * 20 / nowPrice;
            var id = isLong ? Exchange.BuyAsync(-1, buyAmount) : Exchange.SellAsync(-1, buyAmount);
            var auto = new AutoSellOrder();
            auto.SourcePrice = nowPrice;
            //auto.ProfitPrices = cmdVal.sells;
            auto.SourceOrderId = id.Result;
            auto.SourceAmount = buyAmount;
            auto.Amount = buyAmount;
            auto.Direction = isLong ? SideDirection.CloseBuy : SideDirection.CloseSell;
            auto.StopLossPrice = isLong ? nowPrice * 0.95m : nowPrice * 1.05m;  //info.StopLoss;
            auto.HandlerType = nameof(BuyStop); // this.GetType().Name;
            tvCmdEx.autoSellOrders.Add(auto);
            log.Debug("突破压力，交易完成，添加止损：", auto);
        }
        public bool AutoOutHandle(Ticker tck,AutoSellOrder autoSellOrder) {
            if (ISysFun.calcUnitProfit(tck.Buy, autoSellOrder.StopLossPrice,autoSellOrder.Direction== SideDirection.CloseBuy) <0)
            {
                tvCmdEx.execTrade(autoSellOrder.Direction, autoSellOrder.Amount);
                return true;
            }
            return false;
        }
        ILog log = Log.Default();
      
        public override void MainFun()
        {
            
        }


        public List<Info> TmpInfo = new List<Info>();

        

        public class Info
        {
            public SideDirection Direction { get; set; }
            public decimal? StopLoss { get; set; }

        }
    }
}
